package com.glory.brepda.activity.mfg;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.glory.brepda.R;
import com.glory.brepda.adapter.EqpUnitAdapter;
import com.glory.brepda.adapter.wbChangeEqpAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.ResponseMessageEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.entity.wbChangeEqpEntity;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class wbChangeEqpActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.rvEqpList)
    RecyclerView rvEqpList;

    private wbChangeEqpAdapter wbChangeEqpAdapter;

    private boolean canFailFlag = false;





    @Override
    protected int getLayoutResId() {
        return R.layout.activity_wb_change_eqp;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText("提交");
        wbChangeEqpAdapter = new wbChangeEqpAdapter(null);
        initRecyclerView(rvEqpList, this);
        rvEqpList.setAdapter(wbChangeEqpAdapter);
        initEdit(etLotId, this::getEqpList);

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.tvBaseSubmit) {
            if ("".equals(getText(etLotId))){
                showToast("批次号不能为空",false);
                return;
            }
//            StringBuffer result =  new StringBuffer();
//            for (wbChangeEqpEntity data : wbChangeEqpAdapter.getData()) {
//                result.append(data.getEqpList()+Boolean.toString(data.isSelected())+"\n");
//            }
//            showToast(result.toString(),false);
            StringBuffer eqpList = new StringBuffer();
            for (wbChangeEqpEntity data : wbChangeEqpAdapter.getData()) {
                if (data.isSelected()){
                    eqpList.append(data.getEqpList());
                }
            }

            if (!wbChangeEqpAdapter.getData().isEmpty()){
                if ("".equals(eqpList.toString())){
                    showToast("未选择设备号/组",false);
                    return;
                }
                changeEqp(getText(etLotId),eqpList.toString());
            }else if(canFailFlag) {
                AlertDialog dialog=null;
                dialog = new AlertDialog.Builder(wbChangeEqpActivity.this).setTitle("提示")
                        .setMessage("该批次没有符合要求的转机设备，是否继续处理？")
                        .setIcon(R.drawable.lanjian)
                        .setPositiveButton("确定",new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                changeEqpFail(getText(etLotId));
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 取消按钮点击事件
                            }
                        })
                        .create();
                dialog.show();
            }else {
                showToast("不允许提交",false);
            }
//            showToast("当前选择的是"+eqpList,true);
        }

    }
    private void getEqpList(String lotId) {
        FreeUI_EntityCallBack<BaseResponseBean<ResponseMessageEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<ResponseMessageEntity>>
                (new TypeToken<BaseResponseBean<ResponseMessageEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ResponseMessageEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    List<String> eqplist = response.body().getResponse().getBody().getEQPLIST();
                    for (String eqp : eqplist) {
                        wbChangeEqpAdapter.addData(new wbChangeEqpEntity(eqp));
                    }

                }

            }

            @Override
            public void onError(Response<BaseResponseBean<ResponseMessageEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "EQPUNIT");
            }
        };
        canFailFlag=true;
        wbChangeEqpAdapter.getData().clear();
        JsonObject map = new JsonObject();
        map.addProperty("LOTID",lotId);
        map.addProperty("ACTIONTYPE","GET_CHANGE_DATA");
        NetUtils.requestPostNet("IF.WBMATCH","Common", wbChangeEqpActivity.this,map,callBack);
    }

    private void changeEqp(String lotId,String eqpList) {
        FreeUI_EntityCallBack<BaseResponseBean<ResponseMessageEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<ResponseMessageEntity>>
                (new TypeToken<BaseResponseBean<ResponseMessageEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ResponseMessageEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功",true);
                    finish();
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<ResponseMessageEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "EQPUNIT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("LOTID",lotId);
        map.addProperty("EQUIPMENTID",eqpList);
        map.addProperty("ACTIONTYPE","CHANGE_EQP");
        NetUtils.requestPostNet("IF.WBMATCH","Common", wbChangeEqpActivity.this,map,callBack);
    }

    private void changeEqpFail(String lotId) {
        FreeUI_EntityCallBack<BaseResponseBean<ResponseMessageEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<ResponseMessageEntity>>
                (new TypeToken<BaseResponseBean<ResponseMessageEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ResponseMessageEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功",true);
                    finish();
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<ResponseMessageEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "EQPUNIT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("LOTID",lotId);
        map.addProperty("ACTIONTYPE","CHANGE_EQP_FAIL");
        NetUtils.requestPostNet("IF.WBMATCH","Common", wbChangeEqpActivity.this,map,callBack);
    }





}
